using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX.DirectDraw;
using TowerDefense_Dx.Core;

namespace TowerDefense_Dx.GameLogic
{
    public class Tower:DxSprite
    {
        private int _cost ;        
        public int Cost
        {
            get { return _cost; }
            set { _cost = value; }
        }

        private int _damage;

        public int Damage
        {
            get { return _damage; }
            set { _damage = value; }
        }

        private float _range;

        public float Range
        {
            get { return _range; }
            set { _range = value; }
        }
        private double _fireTime;
        private double _elapsedTime;
        private Enemy _target;
        public Enemy Target
        {
            get { return _target; }
        }
        private List<Enemy> _targetList = new List<Enemy>();
        private List<Enemy> _enemyList = new List<Enemy>();
        private List<Bullet> _bulletList = new List<Bullet>();

        public Tower(string imagePath, Device graphicDevice, int frameWidth, int frameHeight, PointF position,float fireTime):
            base(imagePath, graphicDevice, frameWidth, frameHeight)
        {
            _cost = 10;
            _damage = 8;
            _range = 100;
            _position = position;
            _fireTime = fireTime;
            _center.X = _position.X + (_frameWidth / 2);
            _center.Y = _position.Y + (_frameHeight / 2);
        }

        public float GetTargetDistance(PointF targetCenter)
        {
            double deltaDistance;
            deltaDistance = Math.Sqrt(Math.Pow(((double)_center.X - ((double)targetCenter.X)),2)
            + Math.Pow(((double)_center.Y - ((double)targetCenter.Y)),2));            
            return (float)deltaDistance;             
        }
        public void GetTargetList(List<Enemy> enemyList)
        {
        
            for (int i = 0; i < enemyList.Count; i++)
            {
                if (this.GetTargetDistance(enemyList[i].Center) <= _range)
                {
                    _targetList.Add(enemyList[i]);
                }
            }
            if (_targetList != null)
            {
                for (int i = 0; i < _targetList.Count; i++)
                {
                    if (this.GetTargetDistance(_targetList[i].Center) > _range || _targetList[i].EnemyState != Enemy.ESTATE.NORMAL)
                    {
                        _targetList.RemoveAt(i);
                        i--;
                    }
                }
            }
        }
        private void GetClosestEnemy(List<Enemy> enemyList)
        {
            _target = null;           
            if (_targetList.Count > 0)
            {
                _target = _targetList[0];               
            }
            
        }
        private float GetAngle(Enemy target)
        {
            float dx = target.Center.X - this.Center.X;
            float dy = target.Center.Y - this.Center.Y;
            float angle;
            angle = (float)Math.Atan2(dy, dx) * 180.0f / (float)Math.PI;
            return angle;
        }
        private void Attack(double deltaTime)
        {
           
                _elapsedTime += deltaTime;
                float angle = GetAngle(_target);
                if (_elapsedTime >= _fireTime)
                {
                    Bullet b = new Bullet("Sprite/Bullet.png", _graphicDevice, 6, 6, this.Center, angle);
                    _bulletList.Add(b);
                    _elapsedTime = 0;
                }
            
        }
        public void Update(double deltaTime, List<Enemy> enemyList)
        {
            GetTargetList(enemyList);
            GetClosestEnemy(enemyList);
            if (_target != null)
            {
                Attack(deltaTime);               
            }
        }
        public void DrawBullet(Surface destSurface)
        {
            foreach (Bullet bullet in _bulletList)
            {
                bullet.DrawFast((int)bullet.Position.X, (int)bullet.Position.Y,1, destSurface, DrawFastFlags.Wait);
            }
        }
        public void BulletUpdate(Enemy target)
        {            
            for (int i = 0; i < _bulletList.Count; i++)
            {
                _bulletList[i].Update();
                _bulletList[i].CheckColison(target, this.Damage);
                if (GetTargetDistance(_bulletList[i].Center) > _range || _bulletList[i].IsDead == true)
                {
                    _bulletList.Remove(_bulletList[i]);
                    i--;
                }                
            }
        }
    }
}
